﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Vehicle.Web.Pages.Expense
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Expense_Count.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-09-02   Created by 杨光远 V1.00.0000
    /// 功能说明：车队运输成本核算类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Expense_Count : Component.PageBase
    {
        #region---基本变量实例化---
        BLL.Expense b_Expense = new Vehicle.BLL.Expense();
        Model.ComponentsInfo m_ComponentsInfo = new Vehicle.Model.ComponentsInfo();
        Vehicle.BLL.Vehicles b_Vechicles = new Vehicle.BLL.Vehicles();
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        public static float f_AllCount ;//获得总数据
        public static float f_E_personal; //劳务费
        public static float f_E_SCost ;//保险费
        public static float f_E_TCost ;//运输费
        public static float f_E_Incidental ;//杂费
        public static float f_E_All ;//运输总费用
        public string onshow;//定义没有数据时页面的显示内容
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotorcadeDataBind();//绑定车队
                VehiclesDataBind();// 根据车队下拉菜单绑定下属车辆
                f_AllCount = 0;//总费用
                f_E_personal = 0;//劳务费
                f_E_SCost = 0;//保险费
                f_E_TCost = 0;//运输费
                f_E_Incidental = 0;//杂费
                f_E_All = 0;//运输总费用
                ExpenseDataBind();//绑定数据

            }
        }
        /// <summary>
        /// 初始绑定车队
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队
            drop_M_ID.Items.FindByValue("0").Text = "全部";
            drop_M_ID.SelectedValue = "0";

        }
        /// <summary>
        /// 根据车队下拉菜单绑定下属车辆
        /// </summary>
        public void VehiclesDataBind()
        {
            b_Vechicles.DropDownListBind(drop_V_ID, new Vehicle.Model.ComponentsInfo("Vehicles", "M_ID", drop_M_ID.SelectedValue, "0", "0", "V_IsDelete", "1", "V_ID"));
        }
        /// <summary>
        /// 费用数据绑定
        /// </summary>
        public void ExpenseDataBind()
        {
            f_AllCount = 0;
            f_E_personal = 0;
            f_E_SCost = 0;
            f_E_TCost = 0;
            f_E_Incidental = 0;
            f_E_All = 0;
            if (rbt_M_ID.Checked)//按其他条件查询
            {
                m_ComponentsInfo.startIndex = 0;//当前页的起始索引
                m_ComponentsInfo.endIndex = b_Expense.GetAllSearchCount(new Model.ComponentsInfo("Expense", drop_M_ID.SelectedValue == "0" ? "1" : "M_ID", drop_M_ID.SelectedValue == "0" ? "1" : drop_M_ID.SelectedValue, drop_V_ID.SelectedValue == "0" ? "1" : "V_ID", drop_V_ID.SelectedValue == "0" ? "1" : drop_V_ID.SelectedValue, "E_IsDelete", "1", "E_Time", Vehicle.Common.TextHelper.StringToDateTime(this.Gmdp_Start.DateString), Vehicle.Common.TextHelper.StringToDateTime(this.Gmdp_End.DateString), "E_ID"));//当前页的结束索引
                m_ComponentsInfo.tableName = "Expense";//表名
                m_ComponentsInfo.condition = drop_M_ID.SelectedValue == "0" ? "1" : "M_ID";//条件一
                m_ComponentsInfo.result = drop_M_ID.SelectedValue == "0" ? "1" : drop_M_ID.SelectedValue;//匹配结果一
                m_ComponentsInfo.Tcondition = drop_V_ID.SelectedValue == "0" ? "1" : "V_ID";//条件二
                m_ComponentsInfo.Tresult = drop_V_ID.SelectedValue == "0" ? "1" : drop_V_ID.SelectedValue;//匹配结果二
                m_ComponentsInfo.Scondition = "E_IsDelete";//条件三
                m_ComponentsInfo.Sresult = "1";//匹配结果三
                m_ComponentsInfo.Count = "E_Time";//时间
                m_ComponentsInfo.order = "E_ID";//排序
                if (Gmdp_Start.Date != null)//判断起始时间是否为空
                {
                    m_ComponentsInfo.StartTime = Vehicle.Common.TextHelper.StringToDateTime(this.Gmdp_Start.DateString);
                }
                if (Gmdp_End.Date != null)//判断结束时间是否为空
                {
                    m_ComponentsInfo.EndTime = Vehicle.Common.TextHelper.StringToDateTime(this.Gmdp_End.DateString);
                }
                ReptShow.DataSource = b_Expense.GetAllSearch(m_ComponentsInfo).DefaultView;//绑定Repeater控件
                ReptShow.DataBind();
            }
            else if (rbt_T_ID.Checked)//按任务单编号查询
            {
                int t_id = 0; 
                try
                {
                    t_id = Common.TextHelper.StringToInt(b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_Num", txt_T_ID.Text.Trim(), "1", "1", "T_IsDelete", "1", "T_ID")).Rows[0]["T_ID"].ToString());//任务ID
                }
                catch
                {
                }
                ReptShow.DataSource = b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Expense", "T_ID", t_id.ToString(), "1", "1", "E_IsDelete", "1", "E_ID")).DefaultView;
                ReptShow.DataBind();
               
            }
            else if(rbt_C_ID.Checked)//按承运单编号查询
            {
                int c_id = 0;
                try
                {
                    c_id = Common.TextHelper.StringToInt(b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Cargo", "C_Card", txt_C_ID.Text.Trim(), "1", "1", "C_IsDelete", "1", "C_ID")).Rows[0]["C_ID"].ToString());//承运单ID
                  
                }
                catch
                {
                }  
                    ReptShow.DataSource = b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Expense", "C_ID", c_id.ToString(), "1", "1", "E_IsDelete", "1", "E_ID")).DefaultView;
                    ReptShow.DataBind();
               
            }
            lab_ACount.Text = f_AllCount.ToString();//总费用
            lab_AE_All.Text = f_E_All.ToString();//运输总费用
            lab_AE_Incidental.Text = f_E_Incidental.ToString();//杂费
            lab_AE_personal.Text = f_E_personal.ToString();//劳务费
            lab_AE_SCost.Text = f_E_SCost.ToString();//保险费
            lab_AE_TCost.Text = f_E_TCost.ToString();//运输费
            if (ReptShow.Items.Count == 0)//判断没有数据时页面的显示内容
            {
                onshow = "当前页面无数据";
            }
            else
            {
                onshow = "";
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Select_Click(object sender, EventArgs e)
        {
            ExpenseDataBind();
        }
        /// <summary>
        /// Rept绑定数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ReptShow_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                Label lab_C_ID = (Label)e.Item.FindControl("lab_C_ID");//更改承运单编号
                try
                {
                    lab_C_ID.Text = b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Cargo", "C_ID", lab_C_ID.Text, "1", "1", "C_IsDelete", "1", "C_ID")).Rows[0]["C_Card"].ToString();
                }
                catch
                {
                    lab_C_ID.Text = "无";
                }
                Label lab_T_ID = (Label)e.Item.FindControl("lab_T_ID");//更改任务单编号
                try
                {
                    lab_T_ID.Text = b_Expense.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_ID", lab_T_ID.Text, "1", "1", "T_IsDelete", "1", "T_ID")).Rows[0]["T_Num"].ToString();
                }
                catch
                {
                    lab_T_ID.Text = "无";
                }
                f_AllCount++;//增加数据总量

                Label lab_E_personal = (Label)e.Item.FindControl("lab_E_personal");//劳务费
                f_E_personal += Common.TextHelper.StringTofloat(lab_E_personal.Text);//增加劳务费
                Label lab_E_SCost = (Label)e.Item.FindControl("lab_E_SCost");//保险费
                f_E_SCost += Common.TextHelper.StringTofloat(lab_E_SCost.Text);//增加保险费
                Label lab_E_TCost = (Label)e.Item.FindControl("lab_E_TCost");//运输费
                f_E_TCost += Common.TextHelper.StringTofloat(lab_E_TCost.Text);//增加运输费
                Label lab_E_Incidental = (Label)e.Item.FindControl("lab_E_Incidental");//杂费
                 f_E_Incidental  += Common.TextHelper.StringTofloat(lab_E_Incidental.Text);//增加杂费
                 Label lab_E_All = (Label)e.Item.FindControl("lab_E_All");//总费用
                 f_E_All += Common.TextHelper.StringTofloat(lab_E_All.Text);//增加总费用

            }

        }
        /// <summary>
        /// 导出EXCEL文件
        /// </summary>
        /// <param name="dtData"></param>
        public void DataTable2Excel()
        {
            if (ReptShow.Items.Count > 0)
            {
            // 当前对话
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            // IO用于导出并返回excel文件
            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.Default;
            curContext.Response.Charset = "";
            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
            // 返回客户端
            ReptShow.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
             }
            else
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('当前页面无数据,无法导出!')", true);
            }
        }
        /// <summary>
        /// 导出Excel事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Excel_Click(object sender, EventArgs e)
        {
            DataTable2Excel();
        }
        /// <summary>
        /// 车辆下拉菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            VehiclesDataBind();
        }
        #endregion
    }
}
